
//清理请求参数,空字符串和空数组(枚举和时间).
export function clearEmpty(params:any) {
  //Grpc Gateway 清理默认值,Enum,时间等不能空;
  for (const key in params) {
    //递归清理
    if (typeof params[key] === "object") {
      clearEmpty(params[key]);
    }
    //清理空对象
    if (
      typeof params[key] === "object" &&
      Object.keys(params[key]).length === 0
    ) {
      delete params[key];
    }
    //移除空字符
    if (params[key] === "") {
      delete params[key];
    }
  }
  return params;
}


// 获取 URL 参数的函数
export function getQueryParams(): Record<string, string> {
  const params: Record<string, string> = {};
  const search = location.search.substring(1); // 去掉问号
  const pairs = search.split("&");

  for (const pair of pairs) {
    const [key, value] = pair.split("=");
    const decodedKey = decodeURIComponent(key);
    const decodedValue = decodeURIComponent(value);
    params[decodedKey] = decodedValue;
  }
  return params;
}

//深度对象合并.
export function deepMerge(target:any, ...sources:any) {
  if (!sources.length) return target;
  const source = sources.shift();

  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (!target[key]) Object.assign(target, { [key]: {} });
        deepMerge(target[key], source[key]);
      } else {
        Object.assign(target, { [key]: source[key] });
      }
    }
  }

  return deepMerge(target, ...sources);
}

//判断是否时对象.
function isObject(item:any) {
  return item != null && typeof item === 'object';
}